package org.smartagents.gui;

import org.smartagents.body.*;
import org.smartagents.enviro.Environment;
import org.smartagents.enviro.EnvironmentConstants;
import org.smartagents.global.Global;
import org.smartagents.global.GlobalConstants;
import org.smartagents.global.Identifiable;
import org.smartagents.util.CreatureTemplates;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class Main extends JFrame {


    BufferedImage bi = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);

    Graphics2D big;

    int zoom = 4;

    int last_x, last_y;

    boolean firstTime = true;

    Rectangle area;

    private Canvas playground;
    private JPanel mainPanel;
    private Environment enviro = new Environment();

    public Main() throws HeadlessException {

        prepareEnvironment();


        playground = new Canvas();
        playground.setBackground(Color.BLACK);

        mainPanel = new JPanel(new BorderLayout(2, 2));
        mainPanel.add(playground, BorderLayout.CENTER);

        Thread t = new Thread(new Runnable() {
            public void run() {

                System.out.println("trace");
                for (; ;) {
                    drawPlayground();
                    try {
                        Thread.currentThread().sleep(GlobalConstants.REFRESH_DELAY);
                    } catch (InterruptedException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }

            }

        });

        add(mainPanel);
        t.setDaemon(true);
        t.start();


        setSize(800, 600);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setDefaultLookAndFeelDecorated(true);
        pack();

        Thread envT = new Thread(enviro);
        envT.setDaemon(true);
        envT.start();

    }


    private final void drawPlayground() {

        Graphics graphics = playground.getGraphics();
        if (graphics == null)
            return;


        if (firstTime) {
            Dimension dim = playground.getSize();
            int w = dim.width;
            int h = dim.height;
            area = new Rectangle(dim);
            bi = (BufferedImage) createImage(w, h);
            big = bi.createGraphics();
            big.setBackground(Color.BLACK);
            big.setStroke(new BasicStroke(1.0f));
            firstTime = false;
        }

        big.clearRect(0, 0, playground.getWidth(), playground.getHeight());
        for (Identifiable o : enviro.getIdentifiableMap().values()) {
            big.setColor(o.getColor());
            big.fillRect(o.getX() * zoom, o.getY() * zoom, zoom, zoom);
        }
        graphics.drawImage(bi, 0, 0, this);
    }


    private void prepareEnvironment() {
        
        Body body = BodyFactory.buildBody(CreatureTemplates.getBlindExplorer(), 100, 100);

        Identifiable substance = SubstanceFactory.getSubstance("G1&2&&G", 110, 100);

        enviro.addBody(body);
        enviro.addBody(substance);

    }

    public static void main(String[] args) {
        new Main();
    }
}

